package social.hub.impl.jersey.api;

import static social.hub.api.google.orkut.OrkutConstants.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import social.hub.api.ApiConfig;
import social.hub.api.google.orkut.OrkutActivity;
import social.hub.api.google.orkut.OrkutApi;
import social.hub.api.google.orkut.OrkutProfile;
import social.hub.api.google.orkut.util.OrkutFriendsListResponse;
import social.hub.api.google.orkut.util.OrkutProfileResponse;
import social.hub.api.google.orkut.util.OrkutRPCRequest;
import social.hub.impl.jersey.util.ClientFactory;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource.Builder;

public class JerseyOrkutApi implements OrkutApi {

	private static final String[]	DEFAULT_FIELDS	= new String[] {
													"name", "email", "profileUrl", "thumbnailUrl" };
	private static final int		DEFAULT_COUNT	= 1000;

	private static final String		PEOPLE_GET		= "people.get";

	private final Client			client;

	public JerseyOrkutApi(ApiConfig config) {
		client = ClientFactory.createClient(config, OAUTH_SCOPE_FOR_RPC);
	}

	@Override
	public List<OrkutProfile> getFriends() {
		return getFriends(SELF_ID);
	}

	@Override
	public List<OrkutProfile> getFriends(String orkutId) {
		return getFriends(orkutId, DEFAULT_COUNT, DEFAULT_FIELDS);
	}

	@Override
	public List<OrkutProfile> getFriends(String orkutId, int count, String... fields) {

		Map<String, Object> parans = new HashMap<String, Object>();
		parans.put("count", count);
		parans.put("fields", fields);

		OrkutFriendsListResponse response = rpcRequest(PEOPLE_GET, orkutId, FRIENDS_GROUP,
				parans, OrkutFriendsListResponse.class);

		return response.getData().getList();

	}

	@Override
	public OrkutProfile getProfile() {
		return getProfile(SELF_ID);
	}

	@Override
	public OrkutProfile getProfile(String orkutId) {
		return getProfile(orkutId, DEFAULT_FIELDS);
	}

	@Override
	public OrkutProfile getProfile(String orkutId, String... fields) {

		Map<String, Object> parans = new HashMap<String, Object>();
		parans.put("fields", fields);

		OrkutProfileResponse response = rpcRequest(PEOPLE_GET, orkutId, SELF_GROUP,
				parans, OrkutProfileResponse.class);

		return response.getData();

	}

	@Override
	public void updateStatus(String status) {
		updateStatus(status, SELF_ID);
	}

	@Override
	/**
	 * http://www.opensocial.org/Technical-Resources/opensocial-spec-v081/rpc-protocol
	 */
	public void updateStatus(String status, String orkutId) {

		OrkutActivity activity = new OrkutActivity();
		activity.setTitle("Titulo");
		activity.setBody(status);

		Map<String, Object> parans = new HashMap<String, Object>();
		parans.put("activity", activity);

		rpcRequest(PEOPLE_GET, orkutId, SELF_GROUP, parans, null);

	}

	private <T> T rpcRequest(String method, String userId, String groupId,
			Map<String, Object> parans, Class<T> clazz) {

		OrkutRPCRequest rpcData = new OrkutRPCRequest(method, parans);

		rpcData.getParams().put("groupId", groupId);
		rpcData.getParams().put("userId", userId);

		Builder builder = client.resource(ORKUT_BASE_URL_FOR_RPC)
				.type(MediaType.APPLICATION_JSON_TYPE)
				.accept(MediaType.APPLICATION_JSON_TYPE);

		T response = null;

		if (clazz != null) {
			response = builder.post(clazz, rpcData);
		} else {
			builder.post(rpcData);
		}

		return response;

	}

}